package com.leetcode.题库;

/**
 * @author 覃国强
 * @date 2022-03-16 17:48
 */
public class 题库_0014_最长公共前缀 {

  static
  class Solution {
    public String longestCommonPrefix(String[] strs) {
      if (strs == null || strs.length == 0) {
        return "";
      }
      if (strs.length == 1) {
        return strs[0];
      }
      char[] base = strs[0].toCharArray();
      int end = strs[0].length();
      for (int i = 1; i < strs.length; ++i) {
        String str = strs[i];
        if (str == null || str.length() == 0) {
          return "";
        }
        for (int j = 0; j < str.length() && j < end; ++j) {
          if (j == str.length() - 1) {
            end = str.length();
          }
          if (base[j] != str.charAt(j)) {
            end = j;
          }
        }
      }
      return strs[0].substring(0, end);
    }
  }

}
